package com.bjpowernode.jdbc;

import com.mysql.jdbc.Driver;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class JDBCTest5 {
    public static void main(String[] args) {
        //处理查询结果集（遍历结果集）
        Connection conn = null;
        Statement stam = null;
        ResultSet rs = null;
        try {
            //读取配置文件中的数据
            FileInputStream fis = new FileInputStream("C:\\Users\\曾铖坚\\IdeaProjects\\mysql\\connection\\src\\jdbc.propertise");
            Properties properties = new Properties();
            properties.load(fis);
            String driver = (String) properties.get("driver");
            String url = (String) properties.get("url");
            String user = (String) properties.get("user");
            String password = (String) properties.get("password");
            properties.clone();
            fis.close();
            //1.注册驱动
            Class.forName(driver);
            //2.获取连接
            conn = DriverManager.getConnection(url, user, password);
            //3.获取数据库操作对象
            stam = conn.createStatement();
            //4.执行sql语句
            rs = stam.executeQuery("select empno as a,ename,sal from emp");
            //5.处理查询结果集
            while (rs.next()) {
                int empno = rs.getInt("a");
                String ename = rs.getString("ename");
                double sal = rs.getDouble("sal");
                System.out.println(empno + "," + ename + "," + (sal + 100));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //6.释放支援
            try {
                if (stam != null) {
                    stam.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
